package com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.entity.req.*;
import com.xyht.sca_s.student_manage_system.modules.examinePaymentEquipment.service.SmsPaymentExamineEquipmentProcurementService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 付款审批表（设备采购） 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-05-04
 */
@RestController
@RequestMapping("/examinePaymentEquipment")
@SecretBody
public class SmsPaymentExamineEquipmentProcurementController {
    @Autowired
    private SmsPaymentExamineEquipmentProcurementService smsPaymentExamineEquipmentProcurementService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @RequestMapping("/test")
    public ResponseResult test(@RequestBody @Validated SmsPaymentExamineEquipmentProcurementReq req){
        return CommonResult.success(req);
    }

    @ApiModelProperty(value = "提交付款申请")
    @PostMapping("/add")
    public ResponseResult addPaymentExamine(@RequestBody @Validated SmsPaymentExamineEquipmentProcurementReq smsExamineReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsPaymentExamineEquipmentProcurementService.addPaymentExamine(smsExamineReq, user_id);
    }

    @ApiModelProperty(value = "撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelPaymentExamine(@RequestBody SmsPaymentExamineCancelReq cancelReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsPaymentExamineEquipmentProcurementService.cancelPaymentExamine(cancelReq, user_id);
    }

    @ApiModelProperty(value = "审批")
    @PostMapping("/allow")
    public ResponseResult allowPaymentExamine(@RequestBody SmsPaymentExamineAllowReq allowReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsPaymentExamineEquipmentProcurementService.allowPaymentExamine(allowReq, user_id);
    }

    @ApiModelProperty(value = "审批列表")
    @GetMapping("/list")
    public ResponseResult getPaymentExamineList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "examine_type") Integer examine_type,
                                               @RequestParam(value = "search_str",required = false) String search_str,
                                               HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsPaymentExamineEquipmentProcurementService.getPaymentExamineList(user_id,examine_type,search_str,pageNum,pageSize);
    }

    @ApiModelProperty(value = "审批详情")
    @GetMapping("/detail")
    public ResponseResult getPaymentExamineDetail(@RequestParam(value = "examine_id") String examine_id,
                                                     HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsPaymentExamineEquipmentProcurementService.getPaymentExamineDetail(examine_id,user_id);
    }

    @ApiModelProperty(value = "申请人指定流程（申请前获取）")
    @GetMapping("/processList")
    public ResponseResult getPaymentExamineOriginSettingProcess() {
        return smsPaymentExamineEquipmentProcurementService.getPaymentExamineOriginSettingProcess();
    }

    @ApiModelProperty(value = "表格下载")
    @GetMapping("/download")
    public void tableDownload(@RequestParam("examine_id") String examine_id, HttpServletResponse response) {
        try{
            Future<ResponseResult> future = smsPaymentExamineEquipmentProcurementService.tableDownload(examine_id,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ApiModelProperty(value = "历史记录")
    @GetMapping("/history")
    public ResponseResult getExamineHistoryList(
            @RequestParam(value = "real_name",required = false) String real_name,
            @RequestParam(value = "examine_id",required = false) String examine_id,
            @RequestParam(value = "contract_name",required = false) String contract_name,
            @RequestParam(value = "contract_num",required = false) String contract_num,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "status",required = false) Integer status) {
        return smsPaymentExamineEquipmentProcurementService.getExamineHistoryList(real_name,examine_id,contract_name,contract_num,pageNum,pageSize,status);
    }

    @ApiModelProperty(value = "导出历史记录")
    @PostMapping("/exportHistory")
    public ResponseResult exportExamineHistoryList(@RequestBody SmsPaymentExamineEquipmentProcurementHistoryReq req, HttpServletResponse response) {
       ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsPaymentExamineEquipmentProcurementService.exportExamineHistoryList(req, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "强制归档")
    @PostMapping("/forced")
    public ResponseResult forcedFinishPayment(@RequestBody SmsPaymentExamineEquipmentForcedReq forcedReq,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsPaymentExamineEquipmentProcurementService.forcedFinishPayment(forcedReq.getApplyId(),userId);
    }

    @ApiModelProperty(value = "强制流转")
    @PostMapping("/forcedTorsion")
    public ResponseResult forcedTorsionPayment(@RequestBody SmsPaymentExamineEquipmentDetailTorsionReq torsionVO){
        return smsPaymentExamineEquipmentProcurementService.forcedTorsionPayment(torsionVO);
    }

    @ApiModelProperty(value = "代审批")
    @PostMapping("/replaceApply")
    public ResponseResult replaceApplyTorsionPayment(HttpServletRequest request,
                                                     @RequestBody SmsPaymentExamineAllowReq allowVO){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsPaymentExamineEquipmentProcurementService.replaceApplyTorsionPayment(userId,allowVO);
    }

    @ApiModelProperty(value = "强制驳回")
    @PostMapping("/forcedReject")
    public ResponseResult forcedRejectPayment(@RequestBody SmsPaymentExamineEquipmentForcedReq forcedReq, HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsPaymentExamineEquipmentProcurementService.forcedRejectPayment(forcedReq,userId);
    }
}

